<template>
	<view class="Friend">
		<view class="head"></view>
		<view class="search">
			<uni-icons type="back" size="30" color="#000000" class="icon" @click="navigBack"></uni-icons>
			<view class="search-box">
				<up-icon name="search" size="25"></up-icon>
				<input focus placeholder="搜索人和群" class="input" v-model="searchInput" @input="clearInput" />
				<image src="../../static/audio-icons/Enter_delete.png" class="clear_icon" v-if="showClearIcon"
					@click="clearIcon"></image>
			</view>
		</view>
		<scroll-view class="list" :scroll-y="true">
			<view class="l_head">
				<text class="one">你的好友</text>
			</view>
			<view class="item" v-for="item in Friends" @click="Navigator(item)">
				<view class="left">
					<image :src="item.avatar"></image>
					<view class="one">
						<text class="name">{{item.remark}}</text>&nbsp;&nbsp;
						<text class="deta">({{item.account}})</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import { getFriendList } from '../../common/api';
	export default {
		computed: {
			...mapState('info', ['myInfo'])
		},
		data() {
			return {
				searchInput: '', //搜索的内容
				showClearIcon: false,
				Friends: []
			};
		},
		methods: {
			navigBack() {
				uni.navigateBack()
			},
			clearInput(event) {
				this.searchInput = event.detail.value;
				if(event.detail.value == ''){
					this.Friends = [];
					return;
				}
				if(isNaN(this.searchInput)){
					getFriendList({
						userId: this.myInfo.id,
						name: this.searchInput
					}).then((res)=>{
						if(res.code == 200 && res.data != null){
							this.Friends = res.data;
						}else{
							this.Friends = []
						}
					})
				}else{
					getFriendList({
						userId: this.myInfo.id,
						account: this.searchInput,
					}).then((res)=>{
						if(res.code == 200 && res.data != null){
							this.Friends = res.data;
						}else{
							this.Friends = []
						}
					})
				}
				if (event.detail.value.length > 0) {
					this.showClearIcon = true;
				} else {
					this.showClearIcon = false;
				}
			},
			clearIcon() {
				this.searchInput = '';
				this.showClearIcon = false;
			},
			Navigator(item) {
				uni.navigateTo({
					url: '/subpkg/chat_interface/chat_interface?cId='+item.cid+'&nickName='+item.remark
				})
			},
			Add() {
				uni.showLoading();

				setTimeout(() => {
					uni.hideLoading();
					uni.showToast({
						title: '发送成功',
						duration: 1000
					});
				}, 1000);
			}
		}
	}
</script>

<style lang="scss">
	.Friend {
		.head {
			background: linear-gradient(90deg, rgba(122, 220, 241, 1) 0%, rgba(43, 163, 242, 1) 48%);
			width: 750rpx;
			height: 80rpx;
			position: fixed;
		}

		.search {
			position: fixed;
			top: 80rpx;
			display: flex;
			width: 750rpx;
			height: 100rpx;
			align-items: center;
			margin:0 10rpx;

			.search-box {
				display: flex;
				align-items: center;
				background-color: #ececec;
				height: 80rpx;
				width: 500rpx;
				border-radius: 50rpx;
				padding: 0 30rpx;
				margin: 0 50rpx;
				position: relative;

				.clear_icon {
					width: 50rpx;
					height: 50rpx;
					background-color: #2cd1fe;
					border-radius: 50rpx;
					position: absolute;
					right: 30rpx;
				}
			}

			.text {
				color: #339cfe;
				padding: 20rpx;
			}
		}

		.list {
			position: fixed;
			top: 180rpx;
			width: 750rpx;
			height: 90vh;
			background-color: #fff;

			.l_head {
				height: 80rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1rpx solid rgba(224, 224, 224, 0.8);

				.one {
					padding: 0 20rpx;
				}

				.right {
					color: #9c9c9c;
					font-size: 28rpx;
					display: flex;
					align-items: center;
					padding: 0 10rpx;
				}
			}

			.item {
				height: 150rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 20rpx;
				&:active{
					background-color: #d2d2d2;
				}
				.left {
					display: flex;

					image {
						width: 100rpx;
						height: 100rpx;
						border-radius: 50rpx;
					}

					.one {
						display: flex;
						line-height: 20rpx;
						padding: 20rpx;

						.name {
							font-size: 35rpx;
							font-weight: bold;
							color: #2d2d2d;
						}

						.deta {
							font-size: 28rpx;
							color: #9c9c9c;
						}
					}
				}

				.add {
					width: 150rpx;
					height: 60rpx;
					border: 1rpx solid #d2d2d2;
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 10rpx;
				}
			}
		}
	}
</style>